/* ----------------------------------------------------------------------------------------------------------------
 * Scouter
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine 
{
	import flash.display.Stage;
	import flash.geom.Point;
	import pure.engine.entity.Entity;
	import pure.engine.world.WorldBody;
	import pure.engine.world.WorldContent;
	import pure.engine.events.EntityMouseEvent;
	import pure.engine.events.WorldMouseEvent;
	import pure.engine.entity.EntityManager;
	import pure.engine.core.ns_despair;
	use namespace ns_despair;
	
	/**
	 * スカウター
	 * 
	 * @author Pure
	 */
public class Scouter 
{
	public function Scouter(worldBody:WorldBody) 
	{
		m_worldBody = worldBody;
	}
	
	
	public static var cachedPoint:Point = new Point();
	
	public static const HALF_PI:Number            =  Math.PI * 0.5;
	public static const ONE_EIGHTY_DIV_PI:Number  =  180 / Math.PI;
	public static const PI_DIV_ONE_EIGHTY:Number  =  Math.PI / 180;
	
		

	/**
	 * 获取实体
	 * 
	 * @param nameOrList
	 */
	public static function getEntities(nameOrList:*):Array
	{
		var result:Array;
		
		if (nameOrList is String)
		{
			result = EntityManager.getEntitiesByName(nameOrList);
		}
		else if (nameOrList is Array)
		{
			result = EntityManager.getEntitiesByNames(nameOrList);
		}
		return result;
	}
	
	
	// 清除指定名字实体
	public static function clearEntities(nameOrList:*):void
	{
		var list:Array;
		var E:Entity;
		var l:int;
		
		list = getEntities(nameOrList);
		l = list.length;
		while (--l > -1)
		{
			list[l].kill();
		}
	}
	
	
	
	/**
	 * 通过ID获取实体
	 * 
	 * @param entityID
	 */
	public static function getEntityByID(entityID:int):Entity
	{
		return EntityManager.getEntityByID(entityID);
	}
	
	
	/**
	 * 定位
	 * 
	 * @param object (x,y)
	 * @param movement
	 * @param rotation
	 */
	public static function locate(object:Object, movement:Number, rotation:Number):Point
	{
		var angle:Number;

		angle = (rotation - 90) * PI_DIV_ONE_EIGHTY;
		cachedPoint.x = object.x + movement * Math.cos(angle);
		cachedPoint.y = object.y + movement * Math.sin(angle);
		return cachedPoint;
	}
	
	
	/**
	 * 定向
	 * 
	 * [Param] - target    原点(x,y)
	 * [Param] - source    目标(x,y)
	 * [Param] - isRadian  [true]弧度，[false]角度
	 * [Return] - 弧度 / 角度，返回的是原始坐标体系值，非flash坐标体系
	 */
	public static function orientate(source:Object, target:Object, isRadian:Boolean = false):Number
	{
		var angle:Number = Math.atan2(target.y - source.y, target.x - source.x);
		
		return isRadian ? angle + HALF_PI : (angle * ONE_EIGHTY_DIV_PI + 90);
	}
	
	
	/**
	 * 任意拖拽
	 * 
	 * @param W
	 * @param enabled
	 * 
	 * <Hint> Debug用...
	 */
	public static function draggingInWorld(W:WorldContent, enabled:Boolean):void
	{
		var ID:int;
		var oldX:Number, oldY:Number;
		var stage:Stage;
		
		if(enabled)
		{
			W.addEventListener(EntityMouseEvent.MOUSE_DOWN, ____onMouseDown);
		}
		else
		{
			W.removeEventListener(EntityMouseEvent.MOUSE_DOWN, ____onMouseDown);
			____onMouseUp();
		}
		
		function ____onMouseDown(e:WorldMouseEvent):void
		{
			ID = e.entityID;
			oldX = e.worldX;
			oldY = e.worldY;
			W.addEventListener(EntityMouseEvent.MOUSE_MOVE, ____onMouseMove);
			W.addEventListener(EntityMouseEvent.MOUSE_UP, ____onMouseUp);
		}
		
		function ____onMouseMove(e:WorldMouseEvent):void
		{
			var E:Entity

			if (ID)
			{
				E = getEntityByID(ID);
				if (!E)
				{
					ID = 0;
				}
				else
				{
					E.x += e.worldX - oldX
					E.y += e.worldY - oldY;
					oldX = e.worldX;
					oldY = e.worldY;
				}
			}	
			
			if (e.stageX <= 0 || e.stageY <= 0 || e.stageX >= W.stage.stageWidth || e.stageY >= W.stage.stageHeight)
			{
				____onMouseUp();
				return;
			}
			trace(e.worldX + ' | ' + e.worldY);
		}
		
		function ____onMouseUp(e:WorldMouseEvent=null):void
		{
			W.removeEventListener(EntityMouseEvent.MOUSE_MOVE, ____onMouseMove);
			W.removeEventListener(EntityMouseEvent.MOUSE_UP, ____onMouseUp);
			ID = 0;
		}
	}



	
	
	
	
	
	
	
	
	
	
	//======================
	// Member
	//======================
	
	
	private var m_worldBody:WorldBody;
	
}

}